<?php
!defined('P_W') && exit('Forbidden');
$basename="$admin_file?adminjob=modepush";
//@include_once(D_P.'data/bbscache/mode_push_'.$db_mode.'_config.php');
@include_once(D_P.'data/bbscache/mode_push_config.php');
if (empty($action)) {

	include PrintEot('modepush');exit;

} elseif ($action == 'pushlist') {
	@include_once(D_P.'data/bbscache/forum_cache.php');
	@include_once(D_P.'data/bbscache/forumcache.php');
	InitGP(array('key','page','subject','fid','starttime','endtime','m'));
	$starttime && $t_starttime = PwStrtoTime($starttime);
	$endtime && $t_endtime = PwStrtoTime($endtime);
	if (is_numeric($fid) && $fid > 0) {
		$sql .= " AND fid=".pwEscape($fid);
	}
	if ($starttime) {
		$sql.=" AND pushtime>".pwEscape($t_starttime);
	}
	if ($endtime) {
		$sql.=" AND pushtime<".pwEscape($t_endtime);
	}
	if ($subject) {
		$subject = trim($subject);
		$subjectarray = explode(",",$subject);
		foreach ($subjectarray as $value) {
			$value = str_replace('*','%',$value);
			$keywhere .= 'OR';
			$keywhere .= " subject LIKE ".pwEscape("%$value%");
		}
		$keywhere = substr_replace($keywhere,"",0,3);
		$sql .= " AND ($keywhere) ";
	}

	$scr = $PUSH[$key]['scr'];
	(!is_numeric($page) || $page < 1) && $page = 1;
	$limit = pwLimit(($page-1)*$db_perpage,$db_perpage);
	$count = $db->get_value("SELECT COUNT(*) AS sum FROM pw_focus WHERE pushto=".pwEscape($key).$sql);
	$pages = numofpage($count,$page,ceil($count/$db_perpage),"$basename&action=pushlist&key=$key&m=$msubject=".rawurlencode($subjec)."&fid=".$fid."&starttime=".$starttime."&endtime=".$endtime."&");
	$query = $db->query("SELECT id,pushtime,fid,tid,subject,postdate,url FROM pw_focus WHERE pushto=".pwEscape($key)."$sql ORDER BY pushtime DESC ".$limit);
	while ($rt = $db->fetch_array($query)) {
		$rt['pushtime'] = get_date($rt['pushtime'],'Y-m-d H:i');
		$rt['postdate'] = get_date($rt['postdate'],'Y-m-d H:i');
		if ($scr == 'cate') {
			$rt['cateid'] = $cateid = getCateid($rt['fid']);
			$rt['fidname'] = $forum[$cateid]['name'];
		}else{
			$rt['fidname'] = $forum[$rt['fid']]['name'];
		}
		$pushdb[] = $rt;
	}
	include PrintEot('modepush');exit;
} elseif ($action == 'repush' || $action == 'edit') {
	InitGP(array('id','key','m'));
	$fid = $db->get_value("SELECT fid FROM pw_focus WHERE id=".pwEscape($id));
	if (empty($fid)) {
		adminmsg('operate_fail');
	}
	if ($action == 'repush') {
		$db->update("UPDATE pw_focus SET pushtime=".pwEscape($timestamp)." WHERE id=".pwEscape($id));
		$cateid = getCateid($fid);
		$scr = $PUSH[$key]['scr'];
		include_once(Pcv(D_P.'data/bbscache/mode_'.$scr.'_'.$m.'_config.php'));
		$thisconfig = ${$scr."_config"};
		changePushCache($key,$scr,$thisconfig,$m);
		adminmsg('operate_success',$basename."&action=pushlist&key=$key&m=$m");
	} elseif ($action == 'edit') {
		if(empty($_POST['step'])){
			$pushdb = $db->get_one("SELECT id,subject,content,url,imgurl FROM pw_focus WHERE id=".pwEscape($id));
			include PrintEot('modepush');exit;
		}else{
			$scr = $PUSH[$key]['scr'];
			include_once(Pcv(D_P.'data/bbscache/mode_'.$scr.'_'.$m.'_config.php'));
			$thisconfig = ${$scr."_config"};
			InitGP(array('subject','content'));
			InitGP(array('url','imgurl'),'P',0);
			$url 	= str_replace('&#61;','=',$url);
			$imgurl = str_replace('&#61;','=',$imgurl);
			$pwSQL = pwSqlSingle(array(
				'subject'	=> $subject,
				'url'		=> $url,
				'imgurl'	=> $imgurl,
				'content'	=> $content
			));
			$db->update("UPDATE pw_focus SET $pwSQL WHERE id=".pwEscape($id));
			$cateid = getCateid($fid);
			changePushCache($key,$scr,$thisconfig,$m);
			adminmsg('operate_success',$basename."&action=pushlist&key=$key&m=$m");
		}
	}
} elseif ($action == 'del') {
	InitGP(array('selid','key','fids','m'),'P');
	if(empty($selid)) {
		adminmsg('operate_error');
	}
	$fids = array_unique($fids);
	$scr = $PUSH[$key]['scr'];
	include_once(Pcv(D_P.'data/bbscache/mode_'.$scr.'_'.$m.'_config.php'));
	$thisconfig = ${$scr."_config"};
	if ($scr == 'cate') {
		foreach ($fids as $fid) {
			$cateids[] = getCateid($fid);
		}
		$cateids = array_unique($cateids);
		foreach ($cateids as $cateid) {
			changePushCache($key,$scr,$thisconfig,$m);
		}
	} elseif ($scr == 'thread' || $scr == 'read') {
		foreach ($fids as $fid) {
			changePushCache($key,$scr,$thisconfig,$m);
		}
	} else {
		changePushCache($key,$scr,$thisconfig,$m);
	}
	$selid = checkselid($selid);
	$db->update("DELETE FROM pw_focus WHERE id IN($selid)");
	adminmsg('operate_success',$basename."&action=pushlist&key=$key&m=$m");
}

function changePushCache($id,$scr,$thisconfig,$m){
	if ($thisconfig) {
		require_once(R_P.'require/elementcache.php');
		$cachefile = ElementCache::getFileName($scr,$m);
		if ($cachefile) {
			@include(Pcv($cachefile));
			unset($elementsinfo[$id]);
			writeover($cachefile,"<?php\r\n\$elementsinfo = ".pw_var_export($elementsinfo).";\r\n?>");
		}
	}
}
?>